 package org.bb.ssm.controller;  
  
import java.util.List;  
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.bb.ssm.model.Page;
import org.bb.ssm.model.SeeRecord;
import org.bb.ssm.model.UserInfo;  
import org.bb.ssm.service.UserInfoService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.bind.annotation.RequestParam;  
  
@Controller  
@RequestMapping(value="/user")  
public class UserInfoCotroller {  
      
    @Autowired  
    private UserInfoService userInfoService;  
      
    /**  
     * 得到所有用户信息  
     * @param map  
     * @return  
     */  
    @RequestMapping(value="/getAllUser.action")  
    public String getAllUser(Map<String, Object> map){  
        List<UserInfo> userList = userInfoService.findAll();  
        map.put("ALLUSER", userList);  
        return "allUser";  
    }  
    /**  
     * 通过handler前往添加用户页面  
     * @param map  
     * @return  
     */  
    @RequestMapping(value="/addUser.action",method= RequestMethod.GET)  
    public String addUser(Map<String, Object> map){  
        //因为页面使用spring的form标签，其中属性modelAttribute需要存在bean 要不会报错  
        map.put("command", new UserInfo());  
        return "addUser";  
    }  
      
    /**  
     * 添加用户操作  
     * @param userinfo  
     * @return  
     */  
    @RequestMapping(value="/addUser.action",method=RequestMethod.POST)  
    public String save(UserInfo userinfo){  
        int result = userInfoService.insert(userinfo);  
        System.out.println("添加用户的操作结果为："+result);  
        return "redirect:/user/getAllUser";  
    }  
    /**  
     * 删除用户操作  
     * @param id  
     * @return  
     */  
    @RequestMapping(value="/delete/{id}.action",method=RequestMethod.POST)  
    public String delete(@PathVariable(value="id") int id){  
        int result = userInfoService.deleteByPrimaryKey(id);  
        System.out.println("删除用户的操作结果为："+result+"传递进来的id为："+id);  
        System.out.println(id);
        return "redirect:/user/getAllUser.action";  
    }  
    /**  
     * 更新前先根据id找到用户信息，回显到页面上  
     * @param id  
     * @param map  
     * @return  
     */  
   @RequestMapping(value="/updateUser.action",method=RequestMethod.POST)  
    public String update(HttpServletRequest httpServletRequest,String userName,Integer userAge,String userAddress){ 
	    UserInfo userInfo=new UserInfo();
	    int id= (int) httpServletRequest.getSession().getAttribute("uu");
	    userInfo.setUserId(id);
	    userInfo.setUserName(userName);
	    userInfo.setUserAge(userAge);
	    userInfo.setUserAddress(userAddress);
    	userInfoService.updateByPrimaryKey(userInfo);
        System.out.println(id);
        return "redirect:/user/getAllUser.action";  
    }  
   @RequestMapping(value="/updateUser2.action",method=RequestMethod.POST)  
   public String update2(HttpServletRequest httpServletRequest,String userName,Integer userAge,String userAddress){ 
	    UserInfo userInfo=new UserInfo();
	    String id= (String) httpServletRequest.getSession().getAttribute("aa");
	    userInfo.setUserId(Integer.parseInt(id));
	    userInfo.setUserName(userName);
	    userInfo.setUserAge(userAge);
	    userInfo.setUserAddress(userAddress);
   	userInfoService.updateByPrimaryKey(userInfo);
       System.out.println(id);
       return "redirect:/user/getAllUser.action";  
   }  
      @RequestMapping(value="/goUpdateUser/{id}.action")
      public String goUpdateUser(HttpServletRequest httpServletRequest,@PathVariable(value="id") Integer id,Map<String, Object> map) {
  		httpServletRequest.getSession().setAttribute("uu",id);
  		UserInfo userInfo2=userInfoService.selectByPrimaryKey(id);
        map.put("command", userInfo2);  
  		return "updateUser";
  		
  	}
      @RequestMapping(value="/selectByPrimaryKey.action")
      public String selectByPrimaryKey(String search,Map<String, Object>map,HttpServletRequest httpServletRequest) {
    	  System.out.println(search);
    	 UserInfo temp= userInfoService.selectByPrimaryKey(Integer.parseInt(search));
    	 map.put("dd", temp);
    	 httpServletRequest.getSession().setAttribute("aa", search);
    	 
    	 if (temp!=null) {
			return "findUser";
		}
    	  return "";
      }
    @ModelAttribute  
    public void getUserInfo(@RequestParam(value="userId",required=false) Integer id  
            ,Map<String, Object> map){  
        System.out.println("每个controller 方法都会先调用我哦");  
        if(id != null){  
            System.out.println("update 操作");  
            map.put("userInfo", userInfoService.selectByPrimaryKey(id));  
        }  
        System.out.println("insert  操作");  
    }  
      
    @RequestMapping(value="/addUser.action",method=RequestMethod.PUT)  
    public String update(UserInfo userinfo){  
        userInfoService.updateByPrimaryKey(userinfo);  
        return "redirect:/user/getAllUser";  
    } 
    
   /* @RequestMapping(value="/queryAll.action")
    public String record(String loginName , Model model, SeeRecord seeRecord, String pageNow) {
    	String user_Id;
    	Page page=null;
    	user_Id=loginName;
    	int totalCount=userInfoService.getCount(user_Id);
    	if (pageNow!=null&&pageNow!="") {
			
		}
    	return "";
    }*/
}  